<?php
/*
	名片首页
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
$return=array();

cfc('core')->checklogin();

if($_RQ['op']=='main'){
	$mid=intval($_RQ['mid']);
	if(!$mid){
		$mid=$_SESSION['uid'];
	}
	$item=$this->get('card',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$mid));
	if(!$item){
		//没有名片信息，直接跳转名片编辑页
		exi('还未创建名片！','','jy_business_card/info/');
	}
	//检查是否已看过，如果还没看过，此处添加查看数据
	$check=$this->getcolumn('look',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'look_mid'=>$item['mid']),'id');
	if(!$check){
		$new_look=array(
			'uniacid'=>$_SESSION['uniacid'],
			'mid'=>$_SESSION['uid'],
			'look_mid'=>$item['mid'],
			'createtime'=>TIMESTAMP,
		);
		$this->insert('look',$new_look);
	}
	$item['avatar']=$item['avatar']?tomedia($item['avatar']):$this->plugin_resource.'images/header.png';
	$item['bgimg']=$item['bgimg']?tomedia($item['bgimg']):$this->plugin_resource.'images/bgimg/bg1.jpg';
	if($item['mid']==$_SESSION['uid']){
		$return['owner']=true;
	}else{
		//检查本人是否已有微名片
		$check=$this->get('card',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']),array('id'));
		if($check){
			$return['have']=true;
		}
		//检查各项内容是否已设置
		$return['more']=array('home'=>true);
		$more_data=array('presence','photo','company');
		foreach($more_data as $row){
			$check=$this->get($row,array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']),array('id'));
			if($check){
				$return['more'][$row]=true;
			}
		}
		$return['member_secret']=$this->get('member_secret',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$item['mid']));
		$return['member_secret']['paymobile']=1;
		if($return['member_secret']['mobile_fee']>0){
			$return['member_secret']['paymobile']=$this->getcolumn('feelog',array('name'=>'mobile_fee','ispay'=>1,'uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'tomid'=>$item['mid']),'ispay');
		}
	}
	$return['zan']=$this->count('like',array('uniacid'=>$_SESSION['uniacid'],'like_mid'=>$item['mid']));
	$return['renmai']=$this->count('collect',array('collect_mid'=>$item['mid']));
	$return['look']=$this->count('look',array('uniacid'=>$_SESSION['uniacid'],'look_mid'=>$item['mid']));
	$return['item']=$item;
	$sharedata=array(
		'title'=>$item['name'],
		'desc'=>$item['qianming']?:'您好，我是'.$item['name'],
		'link'=>custom_url('jy_business_card/index//mid='.$mid,true),
		'imgUrl'=>$item['avatar'],
	);
	$return['sharedata']=$sharedata;
	exi($return);
}
if($_RQ['op']=='like'){
	$post=get_postdata();
	$mid=intval($post['mid']);
	if(!$mid){
		exi('请先选择要点赞的人~','error');
	}
	$check=$this->get('like',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'like_mid'=>$mid),array('id'));
	if($check){
		exi('您已经点赞过了~','error');
	}
	$like=array(
		'uniacid'=>$_SESSION['uniacid'],
		'mid'=>$_SESSION['uid'],
		'like_mid'=>$mid,
		'createtime'=>TIMESTAMP,
	);
	$this->insert('like',$like);
	exi('success');
}
if($_RQ['op']=='pay'){
	$post=get_postdata();
	$namefee=$post['namefee'];
	$tomid=intval($post['tomid']);
	$member_secret=$this->get('member_secret',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$tomid));
	$price=$member_secret[$namefee];
	if($price>0){
			$data=array(
				'uniacid'=>$_SESSION['uniacid'],
				'mid'=>$_SESSION['uid'],
				'tomid'=>$tomid,
				'name'=>$namefee,
				'price'=>$price,
				'createtime'=>TIMESTAMP,
			);
			pdo_insert('jy_business_card_feelog',$data);
			$id=pdo_insertid();
			$params=array(
					'id'=>$id,
					'title'=>'购买粮食',
					'fee'=>$price,
					'plugin'=>'jy_business_card',
					'no_type'=>array('hdfk'),
					'back_url'=>'jy_business_card/index/payresult/',
					'success_url'=>'jy_business_card/index/',
					'error_url'=>'jy_business_card/index/',
			);
			$payid=cfc('pay')->create_pay_order($params);
			pdo_update('jy_business_card_feelog',array('uniacid'=>$_SESSION['uniacid'],'pay_id'=>$payid),array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
			$res=array(
				'pay_id'=>$payid,
				'order_id'=>$id,
			);
			exi($res);
	}else{
		exi('购买失败，价格未设置！');
	}
}
if($_RQ['op']=='payresult'){
	$payresult=$_SESSION['pay_result'];
	if(!$payresult){
		exi('支付失败，未收到支付结果！','error','main');
	}
	if($payresult['status']!=1){
		exi('支付失败！','error','main');
	}
	$order=pdo_get('jy_business_card_feelog',array('id'=>$payresult['id'],'uniacid'=>$_SESSION['uniacid']));
	if(!$order){
		exi('支付失败！指定订单不存在！','error');
	}
	if($order['status']){
		exi('订单已支付，无需重复支付！','error');
	}
	if($order['price']!=$payresult['fee']){
		exi('支付失败！订单金额不匹配！','error');
	}
	pdo_update('jy_business_card_feelog',array('ispay'=>1),array('uniacid'=>$_SESSION['uniacid'],'id'=>$payresult['id']));
	$this->model('model')->upfee($order);
	exi('支付成功','success','jy_business_card/index/');
}
